package com.webapp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.webapp.model.Application;
import com.webapp.model.ViewApplicationModel;

/**
 * Servlet implementation class EditAppServlet
 */
public class EditAppServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditAppServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session = request.getSession();
		
		Application app = (Application)session.getAttribute("Application");
	
		String numOfDesiredReviews = request.getParameter("desired_reviews");
		
		int desiredReviews = Integer.parseInt(numOfDesiredReviews);
		
		String tierNum = request.getParameter("tier");
		
		float tier = Float.parseFloat(tierNum);
		
		String appSummary = request.getParameter("text_summary");
		String toelfComment = request.getParameter("text_comment");
		
		app.setNumDesiredReviews(desiredReviews);
		app.setTier(tier);
		app.setSummary(appSummary);
		app.setToeflComments(toelfComment);
		
		try {
			ViewApplicationModel vam = new ViewApplicationModel();
			vam.editApplication(app);
			session.setAttribute("Application", app); // get rid of app stored in session
			response.sendRedirect("viewSingleAppsTest.jsp");
		}catch(Exception E){
			List<String> errorList = new ArrayList();
			errorList.add("Database Connection Error");
			errorList.add("Application Model");
			session.putValue("ErrorObject",errorList);
			response.sendRedirect("error.jsp");
		}
	}
}
